package group_1.MultiSnake;

import org.anddev.andengine.opengl.texture.region.TextureRegion;

/**
 * Represents a bullet
 *
 */
public class Bullet extends Item implements SnakeConstants{
	public static final float BULLET_SPEAD = 3;	// the speed of the bullet

	private int moveDirection;					// the direction of which the bullet travels
	private boolean itemInUse = false;			// determines if the bullet is used

	/**
	 * Constructor - construct a new bullet object
	 * 
	 * @param pX The start x-position of the bullet
	 * @param pY The start y-position of the bullet
	 * @param pWidth The width of the bullet
	 * @param pHeight The height of the bullet
	 * @param pTextureRegion Image representing the bullet
	 */
	public Bullet(float pX, float pY, float pWidth, float pHeight, TextureRegion pTextureRegion) {
		super(pX, pY, pWidth, pHeight, pTextureRegion);
	}

	/**
	 * Called when the bullet is fired
	 * 
	 * @param direction The direction that the bullet should travel
	 */
	@Override
	public void use() {
		itemInUse = true;
	}
	
	public void setDirection(int direction) {
		moveDirection = direction;
	}
	
	/**
	 * Called every game-loop
	 * 
	 * Moves the bullet in its direction
	 */
	@Override
	public void loopAction() {
		move();
	}
	
	/**
	 * The bullet is moved in its travel direction if it is used
	 */
	public void move() {
		if(itemInUse) {
			switch(moveDirection) {
			case UP:
				setPosition(this.getX(), this.getY()-BULLET_SPEAD);
				break;
			case DOWN:
				setPosition(this.getX(), this.getY()+BULLET_SPEAD);
				break;
			case RIGHT:
				setPosition(this.getX()+BULLET_SPEAD, this.getY());
				break;
			case LEFT:
				setPosition(this.getX()-BULLET_SPEAD, this.getY());
				break;
			}
		}
	}
}
